package com.hang.gymmanager.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hang.gymmanager.common.BasePageReq;
import com.hang.gymmanager.common.Response;
import com.hang.gymmanager.req.AddCourseReq;
import com.hang.gymmanager.req.BuyCourseReq;
import com.hang.gymmanager.req.DeleteCourseReq;
import com.hang.gymmanager.req.PageCourseReq;
import com.hang.gymmanager.res.CourseDetailRes;
import com.hang.gymmanager.res.PageCourseRes;
import com.hang.gymmanager.res.UserCoursePageRes;
import com.hang.gymmanager.res.userCourseDetailRes;
import com.hang.gymmanager.service.CourseService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 课程控制器
 * @author wyq
 * @version v1.0.0
 * @explain
 * @date 2024/3/20 19:28
 */
@RestController
@RequiredArgsConstructor
@Slf4j
@RequestMapping("/course")
@CrossOrigin
public class CourseController {

    private final CourseService courseService;


    /**
     * 新增课程
     * @param req
     * @return
     */
    @PostMapping("/add")
    public Response<Integer> addCourse(@RequestBody AddCourseReq req){
        Integer id = courseService.addCourse(req);
        return Response.success(id);
    }

    /**
     * 更新课程图片
     * @param file
     * @return
     */
    @PostMapping("/uploadPic")
    public Response<String> uploadPic(@RequestParam("file") MultipartFile file){
        String path = courseService.uploadPic(file);
        return Response.success(path);
    }



    /**
     * 查询课程
     * @param req
     * @return
     */
    @PostMapping("/all")
    public Response<Page<PageCourseRes>> getPageCourse(@RequestBody PageCourseReq req){
        Page<PageCourseRes> pageCourse = courseService.getPageCourse(req);
        return Response.success(pageCourse);
    }

    /**
     * 删除课程
     * @param req
     * @return
     */
    @PostMapping("/delete")
    public Response deleteCourse(@RequestBody DeleteCourseReq req){
        Integer id = req.getId();
        courseService.deleteCourse(id);
        return Response.success();
    }

    /**
     * 课程详情
     * @param courseId
     * @return
     */
    @GetMapping("/detail/{courseId}")
    public Response<CourseDetailRes> detail(@PathVariable Integer courseId){
        CourseDetailRes CourseDetailRes = courseService.detail(courseId);
        return Response.success(CourseDetailRes);
    }


    /**
     * 用户课程列表
     * @return
     */
    @PostMapping("/userCoursePage")
    public Response<Page<UserCoursePageRes>> userCoursePage(@RequestBody BasePageReq req){
        Page<UserCoursePageRes> res = courseService.userCoursePage(req);
        return Response.success(res);
    }

    /**
     * 用户课程详情
     * @param courseId
     * @return
     */
    @GetMapping("/user/detail/{courseId}")
    public Response<userCourseDetailRes> userCourseDetail(@PathVariable Integer courseId){
        userCourseDetailRes res = courseService.userCourseDetail(courseId);
        return Response.success(res);
    }

    /**
     * 用户购买课程
     * @param req
     * @return
     */
    @PostMapping("/buy")
    public Response<String> buy(@RequestBody BuyCourseReq req){
        String buy = courseService.buy(req);
        return Response.success(buy);
    }



}
